{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: h5py in c:\\users\\ridha\\anaconda3\\lib\\site-packages (3.9.0)\n",
      "Requirement already satisfied: numpy>=1.17.3 in c:\\users\\ridha\\anaconda3\\lib\\site-packages (from h5py) (1.24.3)\n"
     ]
    }
   ],
   "source": [
    "!pip install h5py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Variables in the MATLAB file:\n",
      "#refs#\n",
      "Subj_Wins\n"
     ]
    }
   ],
   "source": [
    "import h5py\n",
    "\n",
    "# Specify the path to your .mat file\n",
    "mat_file_path = 'D:/Soumaya/p069903.mat'\n",
    "\n",
    "\n",
    "# Open the MATLAB v7.3 file using h5py\n",
    "with h5py.File(mat_file_path, 'r') as file:\n",
    "    # List all variables in the file\n",
    "    print(\"Variables in the MATLAB file:\")\n",
    "    for name in file:\n",
    "        print(name)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Content of '/Subj_Wins':\n",
      "Variable: ABP_F\n",
      "Variable: ABP_Lag\n",
      "Variable: ABP_Raw\n",
      "Variable: ABP_SPeaks\n",
      "Variable: ABP_Turns\n",
      "Variable: Age\n",
      "Variable: CaseID\n",
      "Variable: ECG_F\n",
      "Variable: ECG_RPeaks\n",
      "Variable: ECG_Raw\n",
      "Variable: Gender\n",
      "Variable: IncludeFlag\n",
      "Variable: PPG_ABP_Corr\n",
      "Variable: PPG_F\n",
      "Variable: PPG_Raw\n",
      "Variable: PPG_SPeaks\n",
      "Variable: PPG_Turns\n",
      "Variable: SegDBP\n",
      "Variable: SegSBP\n",
      "Variable: SegmentID\n",
      "Variable: SubjectID\n",
      "Variable: WinID\n",
      "Variable: WinSeqID\n"
     ]
    }
   ],
   "source": [
    "# Open the MATLAB v7.3 file using h5py\n",
    "with h5py.File(mat_file_path, 'r') as file:\n",
    "    group_name = '/Subj_Wins'\n",
    "    group = file[group_name]\n",
    "\n",
    "    # Print the content of each element in the group\n",
    "    print(f\"Content of '{group_name}':\")\n",
    "    for name in group:\n",
    "        print(f\"Variable: {name}\")\n",
    "      \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "(1, 18)\n",
      "[89.69072165 91.2371134  92.5257732  ... 76.28865979 76.54639175\n",
      " 76.54639175]\n",
      "(1250,)\n",
      "[73.96907216 73.71134021 73.45360825 ... 85.05154639 83.7628866\n",
      " 82.4742268 ]\n",
      "(1250,)\n",
      "[70.10309278 72.93814433 76.28865979 ... 64.17525773 64.94845361\n",
      " 66.49484536]\n",
      "(1250,)\n",
      "[73.96907216 73.19587629 72.68041237 ... 68.29896907 68.04123711\n",
      " 67.78350515]\n",
      "(1250,)\n",
      "[78.86597938 81.95876289 84.79381443 ... 71.39175258 71.13402062\n",
      " 70.6185567 ]\n",
      "(1250,)\n",
      "[70.36082474 69.84536082 69.32989691 ... 61.59793814 63.40206186\n",
      " 65.46391753]\n",
      "(1250,)\n",
      "[66.49484536 68.81443299 70.87628866 ... 62.62886598 62.62886598\n",
      " 62.37113402]\n",
      "(1250,)\n",
      "[57.98969072 57.98969072 57.73195876 ... 66.75257732 66.49484536\n",
      " 66.49484536]\n",
      "(1250,)\n",
      "[68.29896907 68.04123711 68.04123711 ... 66.49484536 66.75257732\n",
      " 67.01030928]\n",
      "(1250,)\n",
      "[67.26804124 67.01030928 66.75257732 ... 88.1443299  89.69072165\n",
      " 90.46391753]\n",
      "(1250,)\n",
      "[61.34020619 61.08247423 60.82474227 ... 73.45360825 73.71134021\n",
      " 73.96907216]\n",
      "(1250,)\n",
      "[70.6185567  70.36082474 69.84536082 ... 70.10309278 70.6185567\n",
      " 71.13402062]\n",
      "(1250,)\n",
      "[74.74226804 74.48453608 74.22680412 ... 63.40206186 63.40206186\n",
      " 63.1443299 ]\n",
      "(1250,)\n",
      "[69.07216495 68.55670103 68.29896907 ... 90.46391753 89.94845361\n",
      " 89.43298969]\n",
      "(1250,)\n",
      "[70.6185567  73.45360825 76.28865979 ... 69.58762887 68.55670103\n",
      " 67.5257732 ]\n",
      "(1250,)\n",
      "[66.49484536 65.46391753 64.69072165 ... 55.92783505 55.92783505\n",
      " 55.67010309]\n",
      "(1250,)\n",
      "[62.5   62.5   62.5   ... 67.5   66.875 66.875]\n",
      "(1250,)\n",
      "[68.125 66.25  65.625 ... 90.625 90.    86.25 ]\n",
      "(1250,)\n"
     ]
    }
   ],
   "source": [
    "\n",
    "with h5py.File(mat_file_path, 'r') as file:\n",
    "    # Access the object reference dataset within the group 'Subj_Wins'\n",
    "    abp_f_refs = file['/Subj_Wins/ABP_Raw'][:]\n",
    "    print(type(abp_f_refs))\n",
    "    print(abp_f_refs.shape)\n",
    "    # Iterate over all object references and dereference them\n",
    "    for ref_array in abp_f_refs:\n",
    "        for ref in ref_array:\n",
    "            # Dereference the object reference\n",
    "            dereferenced_data = file[ref][:][0][:]\n",
    "            print(dereferenced_data)\n",
    "            print(dereferenced_data.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "import h5py\n",
    "import csv\n",
    "\n",
    "# Specify the path to your .mat file\n",
    "mat_file_path = 'D:/Soumaya/p069903.mat'\n",
    "csv_file_path = 'D:/Soumaya/data.csv'\n",
    "def matToCsv(mat_file_path,csv_file_path):\n",
    "# Open the MATLAB v7.3 file using h5py\n",
    "    with h5py.File(mat_file_path, 'r') as file:\n",
    "        # Access the object reference dataset within the group 'Subj_Wins'\n",
    "        ABP_F=file['/Subj_Wins/ABP_F'][:]\n",
    "        var1=list()\n",
    "        for ref_array in ABP_F:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var1.append(dereferenced_data)\n",
    "        print(len(var1))\n",
    "        ABP_Lag=file['/Subj_Wins/ABP_Lag'][:]\n",
    "        var2=list()\n",
    "        for ref_array in ABP_Lag:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var2.extend(dereferenced_data)\n",
    "        ABP_Raw=file['/Subj_Wins/ABP_Raw'][:]\n",
    "        var3=list()\n",
    "        for ref_array in ABP_Raw:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var3.append(dereferenced_data)\n",
    "        ABP_SPeaks=file['/Subj_Wins/ABP_SPeaks'][:]\n",
    "        var4=list()\n",
    "        for ref_array in ABP_SPeaks:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var4.append(dereferenced_data)\n",
    "        ABP_Turns=file['/Subj_Wins/ABP_Turns'][:]\n",
    "        var5=list()\n",
    "        for ref_array in ABP_Turns:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var5.append(dereferenced_data)\n",
    "        Age=file['/Subj_Wins/Age'][:]\n",
    "        var6=list()\n",
    "        for ref_array in Age:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var6.extend(dereferenced_data)\n",
    "        CaseID=file['/Subj_Wins/CaseID'][:]\n",
    "        var7=list()\n",
    "        for ref_array in CaseID:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var7.extend(dereferenced_data)\n",
    "        ECG_F=file['/Subj_Wins/ECG_F'][:]\n",
    "        var8=list()\n",
    "        for ref_array in ECG_F:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var8.append(dereferenced_data)\n",
    "        ECG_RPeaks=file['/Subj_Wins/ECG_RPeaks'][:]\n",
    "        var9=list()\n",
    "        for ref_array in ECG_RPeaks:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var9.append(dereferenced_data)\n",
    "        ECG_Raw=file['/Subj_Wins/ECG_Raw'][:]\n",
    "        var10=list()\n",
    "        for ref_array in ECG_Raw:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var10.append(dereferenced_data)\n",
    "        Gender=file['/Subj_Wins/Gender'][:]\n",
    "        var11=list()\n",
    "        for ref_array in Gender:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var11.extend(dereferenced_data)\n",
    "        IncludeFlag=file['/Subj_Wins/IncludeFlag'][:]\n",
    "        var12=list()\n",
    "        for ref_array in IncludeFlag:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var12.extend(dereferenced_data)\n",
    "        PPG_ABP_Corr=file['/Subj_Wins/PPG_ABP_Corr'][:]\n",
    "        var13=list()\n",
    "        for ref_array in PPG_ABP_Corr:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var13.extend(dereferenced_data)\n",
    "        PPG_F=file['/Subj_Wins/PPG_F'][:]\n",
    "        var14=list()\n",
    "        for ref_array in PPG_F:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var14.append(dereferenced_data)\n",
    "        PPG_Raw=file['/Subj_Wins/PPG_Raw'][:]\n",
    "        var15=list()\n",
    "        for ref_array in PPG_Raw:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var15.append(dereferenced_data)\n",
    "        PPG_SPeaks=file['/Subj_Wins/PPG_SPeaks'][:]\n",
    "        var16=list()\n",
    "        for ref_array in PPG_SPeaks:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var16.append(dereferenced_data)\n",
    "        PPG_Turns=file['/Subj_Wins/PPG_Turns'][:]\n",
    "        var17=list()\n",
    "        for ref_array in PPG_Turns:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var17.append(dereferenced_data)\n",
    "        SegDBP=file['/Subj_Wins/SegDBP'][:]\n",
    "        var18=list()\n",
    "        for ref_array in SegDBP:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var18.extend(dereferenced_data)\n",
    "        SegSBP=file['/Subj_Wins/SegSBP'][:]\n",
    "        var19=list()\n",
    "        for ref_array in SegSBP:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var19.extend(dereferenced_data)\n",
    "        SegmentID=file['/Subj_Wins/SegmentID'][:]\n",
    "        var20=list()\n",
    "        for ref_array in SegmentID:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var20.extend(dereferenced_data)\n",
    "        SubjectID=file['/Subj_Wins/SubjectID'][:]\n",
    "        var21=list()\n",
    "        for ref_array in SubjectID:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var21.extend(dereferenced_data)\n",
    "        WinID=file['/Subj_Wins/WinID'][:]\n",
    "        var22=list()\n",
    "        for ref_array in WinID:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var22.extend(dereferenced_data)\n",
    "        WinSeqID=file['/Subj_Wins/WinSeqID'][:]\n",
    "        var23=list()\n",
    "        for ref_array in WinSeqID:\n",
    "            for ref in ref_array:\n",
    "                # Dereference the object reference\n",
    "                dereferenced_data = file[ref][0]\n",
    "                var23.extend(dereferenced_data)\n",
    "        csv_rows=list()\n",
    "        for i in range(len(var1)):\n",
    "            row_data=list()\n",
    "            row_data.append(var1[i])\n",
    "            row_data.append(var2[i])\n",
    "            row_data.append(var3[i])\n",
    "            row_data.append(var4[i])\n",
    "            row_data.append(var5[i])\n",
    "            row_data.append(var6[i])\n",
    "            row_data.append(var7[i])\n",
    "            row_data.append(var8[i])\n",
    "            row_data.append(var9[i])\n",
    "            row_data.append(var10[i])\n",
    "            row_data.append(var11[i])\n",
    "            row_data.append(var12[i])\n",
    "            row_data.append(var13[i])\n",
    "            row_data.append(var14[i])\n",
    "            row_data.append(var15[i])\n",
    "            row_data.append(var16[i])\n",
    "            row_data.append(var17[i])\n",
    "            row_data.append(var18[i])\n",
    "            row_data.append(var19[i])\n",
    "            row_data.append(var20[i])\n",
    "            row_data.append(var21[i])\n",
    "            row_data.append(var22[i])\n",
    "            row_data.append(var23[i])\n",
    "            csv_rows.append(row_data)\n",
    "        with open(csv_file_path, 'a', newline='') as csvfile:\n",
    "            csv_writer = csv.writer(csvfile)\n",
    "            # Write data rows\n",
    "            csv_writer.writerows(csv_rows)\n",
    "        return max(var4[0])\n",
    "    \n",
    "\n",
    "       \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n",
      "1215.0\n"
     ]
    }
   ],
   "source": [
    "var1=matToCsv(\"p069903.mat\",\"data3.csv\")\n",
    "print(var1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"usual_features.csv\", 'a', newline='') as csvfile:\n",
    "            csv_writer = csv.writer(csvfile)\n",
    "            headline=[\"ABP_SPeaks\",\"ABP_Turns\",\"Age\",\"CaseID\",\"ECG_RPeaks\",\"Gender\",\"IncludeFlag\",\"PPG_SPeaks\",\"PPG_Turns\",\"SegDBP\",\"SegSBP\",\"SegmentID\",\"SubjectID\",\"WinID\",\"WinSeqID\",\"File\"]\n",
    "            # Write data rows\n",
    "            csv_writer.writerow(headline)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CSV file has been created at: D:/Soumaya/data.csv\n"
     ]
    }
   ],
   "source": [
    "# Write the data to a CSV file\n",
    "\n",
    "\n",
    "print(f\"CSV file has been created at: {csv_file_path}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
